#include "AlgorithmLibAdapter.h"
#include <cmath>
//#include "AlgorithmLib.h"
using namespace std;

#include <boost/shared_array.hpp>
using namespace boost;

void ToRaw( CBarProperty* dataSeq, int M, double* d )
{
  for(int i = 0; i < M; ++i)
  {
    d[i] = (*dataSeq)[i];
  }
}

CAlgorithmLibAdapter::CAlgorithmLibAdapter(void)
{
}


CAlgorithmLibAdapter::~CAlgorithmLibAdapter(void)
{
}

//bool CAlgorithmLibAdapter::Check( CBarProperty* dataSeq, int size, bool* error )
//{
//  if(dataSeq->size() < size)
//  {
//    if(error)
//      *error = false;
//    return false;
//  }
//
//  if(error)
//    *error = true;
//
//  return true;
//}

//double CAlgorithmLibAdapter::MA( CBarProperty* dataSeq, int M, bool* error /*= NULL*/ )
//{
//  if(!Check(dataSeq, M, error))
//    return NA_DOUBLE;
//
//  shared_array<double> d(new double[M]);
//  ToRaw(dataSeq, M, d.get());
//  return CAlgorithmLib::MA(d.get(), M);
//}

double CAlgorithmLibAdapter::MA( CBarProperty* dataSeq, int M)
{
  M = M < dataSeq->size() ? M : dataSeq->size();
  shared_array<double> d(new double[M]);
  ToRaw(dataSeq, M, d.get());
  return MA(d.get(), M);
}

double CAlgorithmLibAdapter::STD( CBarProperty* dataSeq, int M )
{
  M = M < dataSeq->size() ? M : dataSeq->size();
  shared_array<double> d(new double[M]);
  ToRaw(dataSeq, M, d.get());
  return STD(d.get(), M);
}

double CAlgorithmLibAdapter::SUM( CBarProperty* dataSeq, int M )
{
  M = M < dataSeq->size() ? M : dataSeq->size();
  shared_array<double> d(new double[M]);
  ToRaw(dataSeq, M, d.get());
  return SUM(d.get(), M);
}

double CAlgorithmLibAdapter::MA( double* d, int M )
{
  double ma = 0;
  for(int i = 0; i < M; ++i)
    ma += d[i];
  return ma /= M;
}

double CAlgorithmLibAdapter::STD( double* d, int M )
{
  double ma = MA(d, M);
  double std = 0;
  for(int i = 0; i < M; ++i)
  {
    std += (d[i]-ma)*(d[i]-ma);
  }
  return sqrt(std/M);
}

double CAlgorithmLibAdapter::SUM( double* d, int M )
{
  double sum = 0;
  for(int i = 0; i < M; ++i)
    sum += d[i];
  return sum;
}

